﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AddToAlbum : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AddToAlbum";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return false;
            }
        }

        public override bool RunStandalone
        {
            get
            {
                return false;
            }
        }


        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Đưa vào album ảnh");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

    
        public override Portal.Interface.WebPartInt[] SubWebParts
        {
            get
            {
                Portal.Interface.WebPartInt[] SubWebParts = new Portal.Interface.WebPartInt[]{
                    
                };
                return SubWebParts;
            }
        }


        public override Portal.Interface.FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("ViewAdvImage",ResourceBLL.GetText(WebPartId,"ViewAdvImage","Xem nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("AddAdvImage",ResourceBLL.GetText(WebPartId,"AddAdvImage","Thêm mới nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("UpdateAdvImageItem",ResourceBLL.GetText(WebPartId,"UpdateAdvImageItem","Cập nhật chi tiết"),WebPartId),
                    new FunctionCls("DeleteAdvImageItem",ResourceBLL.GetText(WebPartId,"DeleteAdvImageItem","Xóa chi tiết"),WebPartId),
                    new FunctionCls("PermissionAdvImage",ResourceBLL.GetText(WebPartId,"PermissionAdvImage","Phân quyền nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("PermissionAdvImageItem",ResourceBLL.GetText(WebPartId,"PermissionAdvImageItem","Phân quyền cho từng bản ghi nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("AddToAlbum",ResourceBLL.GetText(WebPartId,"AddToAlbum","Đưa vào album ảnh"),WebPartId),
                };
            }
        }

        public override Portal.Interface.PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("AdvImageItems","Ảnh quảng cáo website (Items)",new string[]{"PermissionAdvImageItem","UpdateAdvImageItem","DeleteAdvImageItem","AddToAlbum"}),
                    new PermissionTypeCls("AdvImage","Ảnh quảng cáo website",new string[]{"AddAdvImage","PermissionAdvImage"})
                };
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetHtml()
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                string AdvImageId = (string)Environments.Request("AdvImageId");
                if (AdvImageId.Trim().Length == 0)
                {
                    return Environments.GetRecordNotFound();
                }

                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                DataTable dtItems = new AdvImageBLL().ReadingAvailableAlbums(AdvImageCls.belongSite, AdvImageId);
                if (dtItems.Rows.Count == 0)
                {
                    return Environments.GetFormDialog(ResourceBLL.GetText(WebPartId, "AlbumNotFound", "Không có album nào để đưa vào!"),ResourceBLL.GetText(WebPartId,"Title","Đưa bộ ảnh vào album"));
                }
                string selectAlbumText = "<select class=cssSelectAlbum id=drpSelectAlbum>";
                for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                {
                    string AlbumId=(string)dtItems.Rows[iIndex]["AlbumId"];
                    string Subject = "(N/A)";
                    try
                    {
                        Subject = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                    }
                    catch { }
                    selectAlbumText += "<option value=\""+AlbumId+"\">" + Subject + "</option>";
                }
                selectAlbumText += "</select>";
                string RetHtml = "<table>" +
                                 "<tr>" +
                                 "  <td>" + ResourceBLL.GetText(WebPartId, "Album", "Thư viện") + "</td>" +
                                 "  <td>" + selectAlbumText + "</td>" +
                                 "</tr>" +
                                 "<tr>" +
                                 "  <td></td>" +
                                 "  <td><input type=button value=\""+ResourceBLL.GetText(WebPartId,"SubmitButton","Đưa vào album")+"\" onclick=javascript:ClientAdd2Album('"+AdvImageId+"');><input type=button value=\""+ResourceBLL.GetText(WebPartId,"CloseButton","Đóng")+"\" onclick=javascript:window.close();></td>" +
                                 "</tr>" +
                                 "</table>";
                
                
                RetHtml += "</table>";

                RetHtml = Environments.GetFormDialog(RetHtml,ResourceBLL.GetText(WebPartId, "AlbumNotFound", "Không có album nào để đưa vào!"));
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddToAlbum));
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "AdvImage.js") +
                
                "<div id=divAddToAlbumMain class=cssDivAddToAlbum>" + GetHtml() + "</div>";
            return RetHtml;
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideAddToAlbum(string AdvImageId, string AlbumId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImageItems");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddToAlbum", AdvImageId, AdvImageCls.Owner, true);
                new AdvImageBLL().AddAdvImageToAlbum(null,AdvImageCls.belongSite, AdvImageId, AlbumId, AdvImageCls.Subject);
                return ResourceBLL.GetText(WebPartId, "Add2AlbumOk", "Đưa vào album thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}
